package com.example.simplesvc;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.concurrent.ExecutorService;

@Service
public class MySpringService {


    //@Autowired
    ExecutorService executorService;

    @Autowired
    ThreadPoolTaskExecutor taskExecutor;

    private static Logger log = LoggerFactory.getLogger(MySpringService.class);


    public String runWithExecutorService() {

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                for(;;) {
                    log.info("executor task {} processing biz @ {} ", Thread.currentThread().getName(), System.currentTimeMillis());
                    try {
                        Thread.sleep(5000);
                    }catch (Exception e) {
                        //
                    }
                    if(ServerStateHolder.getInstance().isServerStopping()) {
                        return;
                    }
                }
            }
        };

        executorService.submit(runnable);

        return "success";
    }

    public String runWithThreadPoolTaskExecutor() {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                for(;;) {
                    log.info("threadpool task executor task {} processing biz @ {} ", Thread.currentThread().getName(), System.currentTimeMillis());
                    try {
                        Thread.sleep(5000);
                    }catch (Exception e) {
                        //
                    }
                    if(ServerStateHolder.getInstance().isServerStopping()) {
                        return;
                    }
                }
            }
        };

        executorService.submit(runnable);

        return "success";
    }
}
